Jupyter Notebook for R

This tutorial will cover the following:

  • Discussion of Project Jupyter
  • Installation of R in Jupyter Notebook
  • Cell dynamics
  • Keyboard shortcuts for Jupyter Notebook
  • Graphical examples of using R in Jupyter Notebook
  • Saving, converting, and viewing Jupyter Notebook

What is jupyter notebook?

Jupyter notebook is apart of Project Jupyter, which exists for interactive computing across dozens of programming languages.

Project Jupyter

Project Jupyter is a non-profit, open-source project, born out of the IPython Project in 2014 as it evolved to support interactive data science and scientific computing across all programming languages. Jupyter will always be 100% open-source software, free for all to use and released under the liberal terms of the modified BSD license.

project-jupyter

Overview of Jupyter

  1. Supports more than 40 programming languages including Python and R
  2. Notebooks can be shared with others using email, Dropbox, Github, and the Jupyter Notebook Viewer
  3. Produces interactive output with HTML, images, LaTeX, and more
  4. Has big data interaction with Apache Spark using the R or Python language for data exploration

Main features web-based application

  • In-browser editing for code, with automatic syntax highlighting, indentation, and tab completion/introspection.
  • The ability to execute code from the browser, with the results of computations attached to the code which generated them.
  • Displaying the result of computation using rich media representations, such as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures rendered by the matplotlib library, can be included inline.
  • In-browser editing for rich text using the Markdown markup language, which can provide commentary for the code, is not limited to plain text.
  • The ability to easily include mathematical notation within markdown cells using LaTeX, and rendered natively by MathJax.

Three utilites of Project Jupyter

  1. Jupyter Notebook - an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.
  2. JupyterLab - a web-based interactive development environment for Jupyter notebooks, code, and data.
  3. Jupyter Hub - a multi-user version designed for companies, classrooms, or reasearch labs.

Why use jupyter?

  • RStudio is more of a development platform
  • Jupyter notebook has many benefits

Review installation and requirements for the R kernal

Try it in a web broswer

You don't have to install jupyter to try it.

Installing for R

  1. Requirements
    1. Jupyter installed with Anaconda (Python 3.7 version)
    2. Download Anaconda
    3. Follow installation instructions (user level)
    4. Open Anaconda Navigator1
    5. Launch JupyterLab
    6. A current R installation (Everyone should have this!)
  1. Installation
    1. Open the terminal from Jupyter Lab window
    2. Install IRkernel via conda and follow prompts:
      conda install -c r r-irkernel
      conda install -c r r-essentials
      

Notes:

  • Anaconda is great for python and beginers in data science because it comes with manage data science packages installed
  • Anaconda comes with it's own R, and you can install RStudio
    • If you already have R on your computer, you'll need to reinstall all the packages for the conda environment
    • There can be some package management issues in R
    • r-essentials includes packages 80 of the more popular packages (e.g dplyr, shiny, ggplot2, tidyverse, and caret)
  • Anaconda is useful as it installs everything on the user level
  • Anaconda is an isolated environment

1 You can just open Jupyter Notebook, but I like JupyterLab better.

Install without Anaconda

This is much more complicated method. I suggest following the directions below (Windows focused).

Markdown extras

This is an inline equation: $e^{i\pi} + 1 = 0$.

If you want an equation on its own line. $$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$

Keyboard Shortcuts

  • Use Enter to switch to edit mode for a cell.
  • Esc to switch to command mode (not editing a cell).
  • Basic navigation: enter, shift-enter, up/k, down/j
    • up/k and down/j works only in command mode
  • Saving the notebook: ctrl+s
  • Change Cell types (command mode): y, m, 1-6
    • y - changes cell to code cell type
    • m - changes cell to markdown cell type
    • 1-6 - change header level
  • Cell creation (command mode): a, b
    • a - add cell above this one
    • b - add cell below this one
  • Cell editing (command mode): x, c, v
    • x - cut selected cell
    • c - copy selected cell
    • v - paste selected cell
  • Kernel operations (command mode): i, 0 (press twice)
    • i - interrupt kernel
    • 0 - restart kernel

References

Jupyter Lab

  • Markdown reference: Help > Markdown Reference
    • there's an interactive 10 min tutorial to learn Markdown
    • In jupyterlab, not sure if also in jupyter notebook (classic version)
  • Jupyter reference: Help > Jupyter Reference

Jupyter Notebook

  • Keyboard shortcuts: Help > Keyboard Shortcuts
  • Markdown help: Help > Markdown
  • Jupyter help: Help > Notebook Help

Basics in R

Everything you can do in an R terminal, you can do it here!

In [1]:
square <- function(x){ #functions
    return(x*x) 
}

square(8)
64
In [2]:
2 + 2 # fancy calculator
4
In [3]:
print("Hello World!") # print statements
[1] "Hello World!"
In [4]:
a = 10 # variables
print(a)
[1] 10
In [5]:
z = c('Bob', 'Jack', 'Nancy')
for(item in seq_along(z)){ # for loops
    print(z[item])
}
[1] "Bob"
[1] "Jack"
[1] "Nancy"

Plotly in Jupyter Notebook

In [6]:
#install.packages('plotly') # plotly not installed with r-essentials
suppressMessages(library(plotly)) # I do this because I don't like looking at the warnings.
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
embed_notebook(p) # for jupyter lab only